from django.shortcuts import render
from django.views.generic import ListView
from myapp.models import PersonInfo
from django.views.generic import DetailView
from django.views.generic.edit import FormView
from django.views.generic.edit import CreateView
from django.views.generic.edit import UpdateView
from django.views.generic.edit import DeleteView
from django.views.generic import MonthArchiveView
from django.views.generic import  WeekArchiveView
from .form import PersonInfoForm
from django.http import HttpResponse
def myapp(request):
    return render(request, 'myapp.html')
class MyListV(ListView):
    #设置模版文件
    template_name = 'ListV.html'
    #设置模型外的数据
    extra_context = {'title':'人员信息表'}
    #查询模型外的数据
    queryset = PersonInfo.objects.all()
    #每页展示一条数据
    #paginate_by =1
    #若不设置，则模版上下文默认为persioninfo_list
    #context_object_name='personinfo'

class MyDetailV(DetailView):
    #设置模版文件
    template_name = 'DetailV.html'
    #设置模型外的数据
    extra_context = {'title':'人员信息表'}
    #设置模型的查询字段
    slug_field ='age'
    #设置路由的变量名，与属性slug_field实现模型的查询操作
    slug_url_kwarg='age'
    pk_url_kwarg = 'pk'
    #设置查询模型PersonInfo
    model = PersonInfo
    #属性queryset可以做简单的查询操作
    #queryset=PersonInfo.objects.all()
    #若不设置，则模版上下文默认为persioninfo
    #context_object_name='personinfo'
    #是否将pk和slug作为查询条件
    #query_pk_and_slug=False



class MyFormV(FormView):
    initial={'id':1,'name':'Momo','age':18,'hireDate':'2005-12-15'}
    template_name = 'FormV.html'
    success_url = '/result'
    form_class = PersonInfoForm
    extra_context = {'title':'人员信息表'}

def result(request):
    return HttpResponse('Success')

class MyCreateV(CreateView):
    initial={'id':1,'name':'Momo','age':18,'hireDate':'2005-12-15'}
    template_name = 'FormV.html'
    success_url = '/result'
    #表单生成方式一
    #form_class = PersonInfoForm
    #表单生成方式二
    model=PersonInfo
    #fields设置模型字段，从而生成表单字段
    fields = ['id','name','age','hireDate']
    extra_context = {'title':'人员信息表'}

class MyUpdateV(UpdateView):
    template_name = 'UpdateV.html'
    success_url = '/result'
    model = PersonInfo
    #fields设置模型字段，从而生成表单字段
    fields = ['id','name','age','hireDate']
    slug_url_kwarg = 'age'
    slug_field = 'age'
    context_object_name = 'personinfo'
    extra_context = {'title':'人员信息表'}

class MyDeleteV(DeleteView):
    template_name = 'DeleteV.html'
    success_url = '/result'
    model = PersonInfo
    context_object_name = 'personinfo'
    extra_context = {'title': '人员信息表'}

class MyMonthArchiveV(MonthArchiveView):
    allow_empty = True
    allow_future = True
    context_object_name = 'mylist'
    template_name = 'MonthArchiveV.html'
    success_url = '/result'
    model = PersonInfo
    date_field = 'hireDate'
    queryset = PersonInfo.objects.all()
    year_format = '%Y'
    #month_format默认值为%b，支持英文日期，如Oct
    month_format = '%m'
    paginate_by = 50

class MyWeekArchiveV(WeekArchiveView):
    allow_empty = True
    allow_future = True
    context_object_name = 'mylist'
    template_name = 'WeekArchiveV.html'
    model = PersonInfo
    date_field = 'hireDate'
    queryset = PersonInfo.objects.all()
    year_format = '%Y'
    week_format = '%W'
    paginate_by = 50
# Create your views here.

